package com.java.shop.ware.controller;

import com.java.common.exception.BizCodeEnum;
import com.java.common.to.SkuHasStockTo;
import com.java.common.to.WareSkuLockTo;
import com.java.common.utils.PageUtils;
import com.java.common.utils.R;
import com.java.shop.ware.entity.WareSkuEntity;
import com.java.shop.ware.exception.NoStockException;
import com.java.shop.ware.service.WareSkuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 商品库存
 *
 * @author cxq
 * @email 137512476@qq.com
 * @date 2021-04-22 18:22:16
 */
@RestController
@RequestMapping("ware/waresku")
public class WareSkuController {
    @Autowired
    private WareSkuService wareSkuService;


    @PostMapping("/lock/order")
    public R orderLockStock(@RequestBody WareSkuLockTo to){
        try {
            wareSkuService.orderLockStock(to);
            return R.ok();
        } catch (NoStockException e) {
            return R.error(BizCodeEnum.NO_STOCK_EXCEPTION.getCode(),BizCodeEnum.NO_STOCK_EXCEPTION.getMessage()).put(e.getSkuId());
        }
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("ware:waresku:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = wareSkuService.queryPageCondition(params);

        return R.ok().put("page", page);
    }

    /**
     * 根据skuid集合返回sku商品是否有库存
     * @param skuIds
     * @return
     */
    @PostMapping("/hasStock")
    public R hasStock(@RequestBody List<Long> skuIds){
        List<SkuHasStockTo> data = wareSkuService.hasStock(skuIds);
        return R.ok().put(data);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("ware:waresku:info")
    public R info(@PathVariable("id") Long id){
		WareSkuEntity wareSku = wareSkuService.getById(id);

        return R.ok().put("wareSku", wareSku);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("ware:waresku:save")
    public R save(@RequestBody WareSkuEntity wareSku){
		wareSkuService.save(wareSku);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("ware:waresku:update")
    public R update(@RequestBody WareSkuEntity wareSku){
		wareSkuService.updateById(wareSku);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("ware:waresku:delete")
    public R delete(@RequestBody Long[] ids){
		wareSkuService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
